package com.roi.njzk_big_api.quartz;

import cn.hutool.core.collection.CollectionUtil;
import com.roi.njzk_big_api.entity.Electricity;
import com.roi.njzk_big_api.entity.ElectricityCopy1;
import com.roi.njzk_big_api.entity.Water;
import com.roi.njzk_big_api.entity.WaterCopy1;
import com.roi.njzk_big_api.service.IElectricityCopy1Service;
import com.roi.njzk_big_api.service.IElectricityService;
import com.roi.njzk_big_api.service.IWaterCopy1Service;
import com.roi.njzk_big_api.service.IWaterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
@EnableScheduling
public class HourSaveQuartz {
    @Autowired
    private IWaterCopy1Service iWaterCopy1Service;
    @Autowired
    private IWaterService iWaterService;
    @Autowired
    private IElectricityCopy1Service iElectricityCopy1Service;
    @Autowired
    private IElectricityService iElectricityService;

    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void  save(){
        List<Water> waterList = iWaterService.list();
        List<Electricity> electricitieList = iElectricityService.list();
        if(CollectionUtil.isNotEmpty(waterList)){
            for(Water water : waterList){
                WaterCopy1 waterCopy1 = new WaterCopy1();
                waterCopy1.setActualValue(water.getActualValue());
                waterCopy1.setCreateTime(LocalDateTime.now());
                waterCopy1.setDivisionCoefficient(water.getDivisionCoefficient());
                waterCopy1.setIp(water.getIp());
                waterCopy1.setPort(water.getPort());
                waterCopy1.setWaterId(water.getWaterId());
                waterCopy1.setName(water.getName());
                waterCopy1.setTypeName(water.getTypeName());
                waterCopy1.setType(water.getType());
                waterCopy1.setQueryMessage(water.getQueryMessage());
                iWaterCopy1Service.save(waterCopy1);
            }
        }
        if(CollectionUtil.isNotEmpty(electricitieList)){
            for(Electricity electricity : electricitieList){
                ElectricityCopy1 electricityCopy1 = new ElectricityCopy1();
                electricityCopy1.setActualValue(electricity.getActualValue());
                electricityCopy1.setCreateTime(LocalDateTime.now());
                electricityCopy1.setCurrentRatio(electricity.getCurrentRatio());
                electricityCopy1.setDivisionCoefficient(electricity.getDivisionCoefficient());
                electricityCopy1.setElectricityId(electricity.getElectricityId());
                electricityCopy1.setIp(electricity.getIp());
                electricityCopy1.setPort(electricity.getPort());
                electricityCopy1.setName(electricity.getName());
                electricityCopy1.setType(electricity.getType());
                electricityCopy1.setTypeName(electricity.getTypeName());
                electricityCopy1.setQueryMessage(electricity.getQueryMessage());
                iElectricityCopy1Service.save(electricityCopy1);
            }
        }
    }
}
